package com.itheima.mapper;

import com.itheima.bean.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

public class Student_Test{
//没继承接口的类,MyBatis第一天(留作纪念)
    public void update() throws Exception{
        InputStream is = Resources.getResourceAsStream("mybatisconfig/MyBatisConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        Student student = new Student(4,"斯柯达",18,new Date());
        int i = sqlSession.update("StudentMapper.update",student);
        sqlSession.commit();
        is.close();
        sqlSession.close();
    }

    public List<Student> selectAll() throws Exception{
        InputStream is = Resources.getResourceAsStream("mybatisconfig/MyBatisConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<Student> list = sqlSession.selectList("StudentMapper.select");
        for (Student student : list) {
            System.out.println(student);
        }
        is.close();
        sqlSession.close();
        return list;
    }
}
